const express = require("express");
const router = express.Router();
const expressJoi = require("@escook/express-joi");
const { login_schema, reg_schema } = require("../schema/user");
const userHandler = require("../router_handler/user");
const { verifyToken } = require("../util/token");

// 登录接口
router.post("/login", expressJoi(login_schema), userHandler.login);

// 注册接口
router.post("/register", expressJoi(reg_schema), userHandler.regUser);

// 验证token接口
router.post("/verifytoken", (req, res) => {
  const token = req.body.token;

  const result = verifyToken(token);
  if (result) {
    res.send({
      status: 0,
      message: "token验证通过",
    });
  } else {
    res.send({
      status: 1,
      message: "token验证失败",
    });
  }
});

// 获取用户信息接口
router.get("/info/:id", userHandler.getUserInfo);

router.get("/articles/:id", userHandler.getUserArticles);

module.exports = router;
